$(function(){
		layui.use(['form','laydate','layer','util'], function(){
		 	 var form = layui.form;
		 	 var laydate=layui.laydate;
		 	 var layer=layui.layer;
		 	 var util=layui.util;
	 	  		form.render();	
			//重新加载date空间(出生日期)
	 		  laydate.render({
	 	  	  elem:"#birthday",//绑定文本框的id(text)
	 	  	//type:"datetime"//时间类型,年月日，时分秒
			});
			
			var version=null;	
			//调用查询后台角色列表的函数					
				
				//通过用户Id查询用户详情
				function find_currentUserInfo(){
					$.ajax({
						type:"get",
						url:"http://localhost:8080/ssmService/sysUser/findMyUserVo",
						async:false,
						"success":function(result){	
							if(result.resultCode==0) {
								build_currentUserInfo(result);
								version=result.data.user.version;
							}
						}
					});				
				}
				
				//显示编辑按钮信息
				function build_currentUserInfo(result){
				var data=result.data;
						$("input[name='user.account']").val(data.user.account);
						$("input[name='user.name']").val(data.user.name);
						$("input[name='user.phone']").val(data.user.phone);
						$("input[name='user.birthday']").val(util.toDateString(data.user.birthday,"yyyy-MM-dd"));							
						var sex=["input[title='保密']","input[title='男']","input[title='女']"];
						$(sex[data.user.sex]).prop("checked",true);
						$("input[name='user.email']").val(data.user.email);
						var version=data.user.version;
						$("input[name='user.version']").val(data.user.version);
	       			    form.render();
				}
				
				
				form.on("submit(updateUser)",function(data){
					var new_pass=$("input[name='user.password']").val();
					var confirm_pass=$("input[name='confirmPassword']").val();
					if(new_pass==confirm_pass){
						var params=data.field;
						console.log(params);
						
						$.ajax({
							type:"post",
							url:"http://localhost:8080/ssmService/sysUser/updateMyUserVo",
							data:params,
							"success":function(result){
								if(result.resultCode==0){
									layer.msg(result.message,{
										icon:1,
										time:1000
									});
									parent.location.reload();
								}else{
									layer.msg(result.message,{
										icon:1,
										time:1000
									});
								}
							},
							"error":function(){
								layer.msg("修改失败请重试",{
										icon:2,
										time:1000
								});
							}
						});
					}else{
						layer.msg("修改失败,两次输入的密码不一致",{
							icon:2,
							time:1000
						}); 
					}
					return false;
				});
				
				var index = 0;
				$(".updatePassword").click(function() {
					index++;
					console.log(index);
					if(index % 2 != 0) {
						$(".password").empty();
						var passName = ["旧密码:", "新密码:", "确认密码:"];
						var pass_name = ["old_password", "user.password", "confirmPassword"];
						for(var i = 0; i < 3; i++) {
							var bigDiv = $("<div></div>").addClass("layui-form-item");
							var label = $("<label></label>").addClass("layui-form-label").text(passName[i]);
							var minDiv = $("<div></div>").addClass("layui-input-block");
							var passInput = $("<input type='text' name='" + pass_name[i] + "' required  lay-verify='required' placeholder='请输入密码' autocomplete='off' class='layui-input'>")
							minDiv.append(passInput);
							bigDiv.append(label).append(minDiv);
							$(".password").append(bigDiv);
						}
						$(this).text("取消修改密码");
					} else {
						$(".password").empty();
						$(this).text("修改密码");
					}
		
					form.render();
					return false;
				});

				find_currentUserInfo();
		
		});
});

/*$(function(){
		layui.use(['form','laydate','layer','util'], function(){
		 	 var form = layui.form;
		 	 var laydate=layui.laydate;
		 	 var layer=layui.layer;
		 	 var util=layui.util;
	 	  		form.render();	
			//重新加载date空间(出生日期)
	 		  laydate.render({
	 	  	  elem:"#birthday",//绑定文本框的id(text)
	 	  	  type:"date"//时间类型,年月日，时分秒
			});
			
			var version=null;		
				//通过用户Id查询用户详情
				function find_currentUserInfo(){
					$.ajax({
						type:"get",
						url:"http://localhost:8080/ssmService/sysUser/findMyUserVo",
						async:true,
						"success":function(result){	
							if (result.resultCode==0) {
								build_currentUserInfo(result);
								version=result.data.user.version;
								
							}
						}
					});				
				}
				
				//显示编辑按钮信息
				function build_currentUserInfo(result){
					var data=result.data;
						$("input[name='user.account']").val(data.user.account);
						$("input[name='user.name']").val(data.user.name);
						$("input[name='user.phone']").val(data.user.phone);
						$("input[name='user.birthday']").val(util.toDateString(data.user.birthday,"yyyy-MM-dd"));							
						var sex=["input[title='保密']","input[title='男']","input[title='女']"];
						$(sex[data.user.sex]).prop("checked",true);
						$("input[name='user.email']").val(data.user.email);
						var version=data.user.version;
						$("input[name='user.version']").val(data.user.version);
	       			    form.render();
				}
				
				form.on("submit(updateUser)",function(data){
					var new_password=$("input[name='user.password']").val();
					var conf_password=$("input[name='confirm_password']").val();
					if(new_password==conf_password){
						var params=data.field;
					$.ajax({
						type:"post",
						url:"http://localhost:8080/ssmService/sysUser/updateMyUserVo",
						data:params,
						"success":function(result){
							if(result.resultCode==0){
								layer.msg(result.message,{			
									icon:1,
									time:5000
								});
								//获取当前弹出层，通过父页面获取当前frame框的索引即窗口名称
								//var index=parent.layer.getFrameIndex(window.name);						
								parent.location.reload();//刷新父页面
								//parent.layer.close(index);//关闭弹出层
							}else{
								layer.msg(result.message,{
									icon:2,
									time:1000
								});																														
							}
						},
						"error":function(){
							layer.msg("修改失败！",{
										icon:2,
										time:1000
								});
						}
					});
					}else{
						layer.msg("修改失败，两次输入的密码不一致！",{
										icon:2,
										time:1000
								});
					}
					return false;
				});
				
				var index=0;
				$(".updatePassword").click(function(){
					index++;
					if(index%2!=0){
					$(".password").empty();
					var passName=["旧密码：","新密码：","确认密码："];
					var pass_name=["old_password","user.password","confirm_password"];
					for(var i=0;i<3;i++){		
					var bigDiv=$("<div></div>").addClass("layui-form-item");
					var label=$("<label></label>").addClass("layui-form-label").text(passName[i]);
					var minDiv=$("<div></div>").addClass("layui-input-block");
					var passInput=$("<input type='text' name='"+pass_name[i]+"' required  lay-verify='required' placeholder='请输入密码' autocomplete='off' class='layui-input'>");
					minDiv.append(passInput);
					bigDiv.append(label).append(minDiv);
					$(".password").append(bigDiv);
					}
					$(this).text("取消修改密码");
				}else{
					$(".password").empty();
					$(this).text("修改密码");
				}
					form.render();
					return false;
				});
				
					
			//调用查询后台角色列表的函数					
			find_currentUserInfo();
		
		});
});*/